<?php

namespace addons\helper\controller\wxapp\helper;

use addons\helper\controller\wxapp\Base;

use fast\Tree;

class Menu extends Base
{
    protected $rulelist = [];
    protected $menuList = [];
    protected $config = [
        'model'        => '菜单',
        'limit'        => 0,
        'defaultSort'  => 'id',
        'defaultOrder' => 'desc',
        'multi'        => true,
        'field' => [
            'isshow'     => ['显示', 'radio', false,  false, '', true, true, true],
            'name'       => ['规则', 'text', false,  false, '', true, true, true],
            'title'      => ['标题', 'text', false,  false, '', true, true, true],
            'icon'       => ['图标', 'text', false,  false, '', true, true, true],
        ],
        'fieldContent' => [
            'isshow' => [
                '1' => '显示',
                '0' => '隐藏'
            ],
        ],
        'auth' => [
            'multi' => [
                'hidden'  => [
                    'name'        => '设为隐藏',
                    'color'       => '#f39c12',
                    'icon'        => 'fa-eye-slash',
                    'url'         => 'helper.menu/multi',
                    'params'      => 'isshow=0'
                ],
                'normal'  => [
                    'name'        => '设为正常',
                    'color'       => '#18bc9c',
                    'icon'        => 'fa-eye',
                    'url'         => 'helper.menu/multi',
                    'params'      => 'isshow=1'
                ],
                
            ],
            'more' => [
                'hidden'  => [
                    'name'        => '设为隐藏',
                    'color'       => '#f39c12',
                    'url'         => 'helper.menu/multi',
                    'params'      => 'isshow=0'
                ],
                'normal'  => [
                    'name'        => '设为正常',
                    'color'       => '#18bc9c',
                    'url'         => 'helper.menu/multi',
                    'params'      => 'isshow=1'
                ],
            ],
        ],
    ];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\AuthRule;
        $this->config['field'] = $this->formatField();
        $this->config['pk'] = $this->model->getPk() ? $this->model->getPk() : 'id';
        // 必须将结果集转换为数组
        $ruleList = collection($this->model->where('ismenu', 1)->where('id','<>',1)->where('status', 'normal')->field('condition,remark,createtime,updatetime', true)->order('weigh DESC,id ASC')->select())->toArray();
        foreach ($ruleList as $k => &$v) {
            $v['title'] = __($v['title']);
        }
        unset($v);
        $menuList = get_addon_config('helper')['MenuList'];
        $this->menuList = explode(',', $menuList);
        Tree::instance()->init($ruleList,null,'　');
        $this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
        foreach ($this->rulelist as $k => &$v) {
            if (!$v['ismenu']) {
                continue;
            }
            $v['isshow'] = in_array($v['id'],$this->menuList) ? 1 : 0;
        }
        unset($v);
    }

    public function index()
    {
        if ($this->request->isPost()) {
            $list = $this->rulelist;
            $total = count($this->rulelist);

            $result = array('total' => $total, 'rows' => $list);

            $data = [
                'tabList'  => null,
                'dataList' => $result,
                'config'   => $this->config
            ];
            $this->success('ok', $data);
        }
    }

    public function multi()
    {
        $ids = $this->request->post('ids/a');
        if ($ids) {
            if ($this->request->has('params')) {
                parse_str($this->request->post('params'), $values);
                if (isset($values['isshow'])) {
                    $ids = $values['isshow'] ? array_unique(array_merge($ids,$this->menuList)) : array_diff($this->menuList,$ids);

                    $result = set_addon_config('helper', ['MenuList' => implode(',',$ids)]);
                    if ($result) {
                        $this->success();
                    } else {
                        $this->error();
                    }
                }
            }
        }
    }
}
